// Created by WXX on 2021/10/3 11:45
#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

/**
 * 执行用时：72 ms, 在所有 C++ 提交中击败了77.13%的用户
 * 内存消耗：36.7 MB, 在所有 C++ 提交中击败了27.04%的用户
 */
class Solution {
public:
    vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
        unordered_map<string, int> hash;
        for (int i = 0; i < list2.size(); i++) hash[list2[i]] = i;
        vector<string> res;
        int index = 1e8;
        for (int i = 0; i < list1.size(); i++) {
            string a = list1[i];
            if (hash.count(a)) {
                if (i + hash[a] < index) {
                    res.clear();
                    index = i + hash[a];
                    res.push_back(a);
                } else if (i + hash[a] == index) {
                    res.push_back(a);
                }
            }
        }
        return res;
    }
};

void OutputBasicArray1D(vector<string> nums) {
    cout << "[";
    for (int i = 0; i < nums.size(); i++) {
        cout << nums[i];
        if (i != nums.size() - 1) cout << ", ";
    }
    cout << "]" << endl;
}

int main() {

    vector<string> a = {"Shogun", "Tapioca Express", "Burger King", "KFC"};
    vector<string> b = {"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"};

    OutputBasicArray1D(Solution().findRestaurant(a, b));

    return 0;
}
